package com.celeste.mcpserverfeishu.controller;

import com.celeste.mcpserverfeishu.domain.model.DocumentRequest;
import com.celeste.mcpserverfeishu.domain.model.DocumentResponse;
import com.celeste.mcpserverfeishu.dto.block.CreateBlockResponse;
import com.celeste.mcpserverfeishu.dto.document.CreateDocumentResponse;
import com.celeste.mcpserverfeishu.service.FeishuService;
import com.celeste.mcpserverfeishu.util.FeishuDocumentHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.web.bind.annotation.RestController;

/**
 * 飞书文档控制器，提供工具方法让大模型调用
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class FeishuDocumentController {

    private final FeishuService feishuService;
    private final FeishuDocumentHelper feishuDocumentHelper;

    /**
     * 创建飞书文档并添加内容
     *
     * @param request 包含访问令牌、文档标题和内容的请求
     * @return 文档响应
     */
    @Tool(description = "创建飞书文档，返回文档ID，参数：访问令牌(必填)，文档标题(必填)，文档内容(选填)")
    public DocumentResponse createDocument(DocumentRequest request) {
        try {
            log.info("创建飞书文档，标题:{}", request.getTitle());
            String documentId = feishuDocumentHelper.createDocumentWithContent(
                    request.getAccessToken(), 
                    request.getTitle(), 
                    request.getContent()
            );
            return DocumentResponse.builder()
                    .success(true)
                    .documentId(documentId)
                    .message("文档创建成功")
                    .build();
        } catch (Exception e) {
            log.error("创建飞书文档失败", e);
            return DocumentResponse.builder()
                    .success(false)
                    .message("文档创建失败: " + e.getMessage())
                    .build();
        }
    }

    /**
     * 获取飞书文档内容
     *
     * @param request 包含访问令牌和文档ID的请求
     * @return 文档响应
     */
    @Tool(description = "获取飞书文档内容，参数：访问令牌(必填)，文档ID(必填)")
    public DocumentResponse getDocumentContent(DocumentRequest request) {
        try {
            log.info("获取飞书文档内容，文档ID:{}", request.getDocumentId());
            String content = feishuDocumentHelper.readDocument(
                    request.getAccessToken(), 
                    request.getDocumentId()
            );
            return DocumentResponse.builder()
                    .success(true)
                    .documentId(request.getDocumentId())
                    .content(content)
                    .message("获取文档内容成功")
                    .build();
        } catch (Exception e) {
            log.error("获取飞书文档内容失败", e);
            return DocumentResponse.builder()
                    .success(false)
                    .documentId(request.getDocumentId())
                    .message("获取文档内容失败: " + e.getMessage())
                    .build();
        }
    }

    /**
     * 向文档追加内容
     *
     * @param request 包含访问令牌、文档ID和内容的请求
     * @return 文档响应
     */
    @Tool(description = "向飞书文档追加内容，参数：访问令牌(必填)，文档ID(必填)，要追加的内容(必填)")
    public DocumentResponse appendContent(DocumentRequest request) {
        try {
            log.info("向飞书文档追加内容，文档ID:{}", request.getDocumentId());
            feishuDocumentHelper.appendContent(
                    request.getAccessToken(), 
                    request.getDocumentId(), 
                    request.getContent()
            );
            return DocumentResponse.builder()
                    .success(true)
                    .documentId(request.getDocumentId())
                    .message("内容已成功追加到文档")
                    .build();
        } catch (Exception e) {
            log.error("向飞书文档追加内容失败", e);
            return DocumentResponse.builder()
                    .success(false)
                    .documentId(request.getDocumentId())
                    .message("追加内容失败: " + e.getMessage())
                    .build();
        }
    }
} 